package com.travel.controller.admin;

import com.travel.constant.ApiConstants;
import com.travel.model.dto.common.ApiResponse;
import com.travel.model.entity.content.Post;
import com.travel.service.Service.PostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
//管理员文章管理接口 , 获取所有文章, 删除文章, 更新文章状态
@Tag(name = "管理员文章", description = "管理员文章管理接口")
@RestController
@RequestMapping(ApiConstants.API_ADMIN + "/posts")
@RequiredArgsConstructor
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
public class AdminPostController {
    
    private final PostService postService;

    @Operation(summary = "获取所有文章", description = "管理员获取所有文章")
    @GetMapping
    public ApiResponse<Page<Post>> getAllPosts(
            @Parameter(description = "分页信息") Pageable pageable) {
        Page<Post> posts = postService.getAllPosts(pageable);
        return ApiResponse.success("获取成功", posts);
    }

    @Operation(summary = "删除文章", description = "管理员删除文章")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deletePost(
            @Parameter(description = "文章ID") @PathVariable Long id) {
        postService.deletePost(id);
        return ApiResponse.success("删除成功");
    }

    @Operation(summary = "更新文章状态", description = "管理员更新文章状态")
    @PutMapping("/{id}/status")
    public ApiResponse<Post> updatePostStatus(
            @Parameter(description = "文章ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam Integer status) {
        postService.updatePostStatus(id, status);
        Post post = postService.getPost(id);
        return ApiResponse.success("更新成功", post);
    }
} 